<?php

/*
 * FileSender www.filesender.org
 *
 * Copyright (c) 2009-2012, AARNet, Belnet, HEAnet, SURFnet, UNINETT
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * *    Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * *    Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * *    Neither the name of AARNet, Belnet, HEAnet, SURFnet and UNINETT nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// Require environment (fatal)
if (!defined('FILESENDER_BASE')) {
    die('Missing environment');
}

/**
 * Mime type handling
 */
class Mime
{
    /**
     * Dictionary
     */
    private static $mimeexts = array(
        '123' => 'application/vnd.lotus-1-2-3',
        '3dm' => 'x-world/x-3dmf',
        '3dmf' => 'x-world/x-3dmf',
        '3gp' => 'video/3gp',
        '3gpp' => 'audio/3gp',
        '7z' => 'application/x-7z-compressed',
        'aab' => 'application/x-authorware-bin',
        'aac' => 'audio/aac',
        'aam' => 'application/x-authorware-map',
        'aas' => 'application/x-authorware-map',
        'adr' => 'application/x-msaddr',
        'afl' => 'video/animaflex',
        'afm' => 'application/x-font-type1',
        'ai' => 'application/postscript',
        'aif' => 'audio/x-aiff',
        'aifc' => 'audio/x-aiff',
        'aiff' => 'audio/x-aiff',
        'alt' => 'application/x-up-alert',
        'aos' => 'application/x-nokia-9000-communicator-add-on-software',
        'arj' => 'application/x-arj',
        'asc' => 'text/plain',
        'asd' => 'application/astound',
        'asf' => 'video/x-ms-asf',
        'asn' => 'application/astound',
        'asp' => 'application/x-asap',
        'asx' => 'video/x-ms-asf',
        'asz' => 'application/astound',
        'atom' => 'application/atom+xml',
        'au' => 'audio/basic',
        'avi' => 'video/avi',
        'axs' => 'application/olescript',
        'bas' => 'text/plain',
        'bat' => 'text/plain',
        'batfrag' => 'text/plain',
        'bcpio' => 'application/x-bcpio',
        'bild' => 'image/jpeg',
        'bin' => 'application/octet-stream',
        'bmp' => 'image/bmp',
        'btmfrag' => 'text/plain',
        'cacert' => 'application/x-x509-ca-cert',
        'ccs' => 'text/ccs',
        'cdf' => 'application/x-netcdf',
        'cdr' => 'application/x-cdr',
        'cer' => 'application/x-x509-ca-cert',
        'cgm' => 'image/cgm',
        'chat' => 'application/x-chat',
        'che' => 'application/x-up-cacheop',
        'chm' => 'application/chm',
        'cht' => 'audio/x-dspeech',
        'class' => 'application/java-vm',
        'clp' => 'image/x-clp',
        'cmd' => 'text/plain',
        'cmx' => 'image/x-cmx',
        'cnc' => 'application/x-cnc',
        'cod' => 'image/cis-cod',
        'coda' => 'application/x-coda',
        'com' => 'application/octet-stream',
        'con' => 'application/x-connector',
        'cpi' => 'image/cpi',
        'cpio' => 'application/x-cpio',
        'cpt' => 'application/mac-compactpro',
        'crt' => 'application/x-x509-ca-cert',
        'csh' => 'application/x-csh',
        'csm' => 'application/x-cu-seeme',
        'css' => 'text/css',
        'cssfrag' => 'text/plain',
        'csv' => 'text/comma-separated-values',
        'cu' => 'application/x-cu-seeme',
        'cut' => 'image/x-halo-cut',
        'dbf' => 'application/octet-stream',
        'dcr' => 'application/x-director',
        'der' => 'application/x-x509-ca-cert',
        'dig' => 'multipart/mixed',
        'dir' => 'application/x-director',
        'djv' => 'image/vnd.djvu',
        'djvu' => 'image/vnd.djvu',
        'dll' => 'application/octet-stream',
        'dmg' => 'application/octet-stream',
        'dms' => 'application/octet-stream',
        'doc' => 'application/msword',
        'dsf' => 'image/x-mgx-dsf',
        'dss' => 'text/dss',
        'dst' => 'application/tajima',
        'dtd' => 'application/xml-dtd',
        'dus' => 'audio/x-dspeech',
        'dvi' => 'application/x-dvi',
        'dwf' => 'drawing/x-dwf',
        'dwg' => 'image/vnd',
        'dxf' => 'image/vnd',
        'dxr' => 'application/x-director',
        'ear' => 'application/java-archive',
        'ebk' => 'application/x-expandedbook',
        'ecert' => 'application/x-x509-email-cert',
        'email' => 'application/x-x509-email-cert',
        'emf' => 'image/x-emf',
        'eml' => 'message/rfc822',
        'eot' => 'application/vnd.ms-fontobject',
        'eps' => 'application/postscript',
        'erf' => 'application/x-hsp-erf',
        'es' => 'audio/echospeech',
        'etf' => 'image/x-etf',
        'etx' => 'text/x-setext',
        'evy' => 'application/envoy',
        'exe' => 'application/octet-stream',
        'ez' => 'application/andrew-inset',
        'fh4' => 'image/x-freehand',
        'fh5' => 'image/x-freehand',
        'fhc' => 'image/x-freehand',
        'fif' => 'image/fif',
        'flv' => 'video/x-flv',
        'fml' => 'application/fml',
        'fpx' => 'image/x-fpx',
        'frl' => 'application/freeloader',
        'fs' => 'application/X-FSRecipe',
        'gif' => 'image/gif',
        'gram' => 'application/srgs',
        'grxml' => 'application/srgs+xml',
        'gsd' => 'audio/gsm',
        'gsm' => 'audio/gsm',
        'gtar' => 'application/x-gtar',
        'gvi' => 'application/x-gsp',
        'gvp' => 'application/x-gsp',
        'gz' => 'application/gzip',
        'hdf' => 'application/x-hdf',
        'hdml' => 'text/x-hdml',
        'hlp' => 'application/octet-stream',
        'hqx' => 'application/mac-binhex40',
        'htm' => 'text/html',
        'html' => 'text/html',
        'htmlfrag' => 'text/plain',
        'ica' => 'application/x-ica',
        'ice' => 'x-conference/x-cooltalk',
        'ico' => 'image/x-icon',
        'ics' => 'text/calendar',
        'ief' => 'image/ief',
        'ifb' => 'text/calendar',
        'iges' => 'model/iges',
        'igs' => 'model/iges',
        'ins' => 'application/x-NET-Install',
        'ips' => 'application/ips',
        'ipx' => 'application/x-ipix',
        'ivr' => 'i-world/i-vrml',
        'jad' => 'text/vnd.sun.j2me.app-descriptor',
        'jar' => 'application/java-archive',
        'jar.pack.gz' => 'application/x-java-pack200',
        'jardiff' => 'application/x-java-archive-diff',
        'java' => 'text/x-java-source',
        'javafrag' => 'text/plain',
        'jnlp' => 'application/x-java-jnlp-file',
        'jnlpfrag' => 'text/plain',
        'jp2' => 'image/jp2',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'jps' => 'image/x-jps',
        'js' => 'application/x-javascript',
        'kar' => 'audio/midi',
        'latex' => 'application/x-latex',
        'lha' => 'application/octet-stream',
        'lit' => 'application/lit',
        'lsp' => 'text/lsp',
        'lwp' => 'application/x-wordpro',
        'lzh' => 'application/octet-stream',
        'lzx' => 'application/octet-stream',
        'm3u' => 'audio/x-mpegurl',
        'm4a' => 'audio/mp4',
        'm4u' => 'video/vnd.mpegurl',
        'man' => 'application/x-troff-man',
        'map' => 'application/x-httpd-imap',
        'mathml' => 'application/mathml+xml',
        'mbd' => 'application/mbedlet',
        'mcf' => 'image/vasa',
        'mdb' => 'application/mdb',
        'me' => 'application/x-troff-me',
        'mesh' => 'model/mesh',
        'mfp' => 'application/mirage',
        'mid' => 'audio/x-midi',
        'midi' => 'audio/x-midi',
        'mif' => 'application/x-mif',
        'mol' => 'chemical/x-mdl-molfile',
        'mov' => 'video/quicktime',
        'movie' => 'video/x-sgi-movie',
        'mp2' => 'audio/mpeg',
        'mp3' => 'audio/mp3',
        'mp4' => 'video/mp4',
        'mpe' => 'video/mpeg',
        'mpeg' => 'video/mpeg',
        'mpg' => 'video/mpeg',
        'mpga' => 'audio/mpeg',
        'mpire' => 'application/x-mpire',
        'mpl' => 'application/x-mpire',
        'mpp' => 'application/vnd.ms-project',
        'ms' => 'application/x-troff-ms',
        'msh' => 'model/mesh',
        'msi' => 'application/msi',
        'msm' => 'application/msm',
        'mxu' => 'video/vnd.mpegurl',
        'n2p' => 'application/n2p',
        'nc' => 'application/x-netcdf',
        'npx' => 'application/x-netfpx',
        'nsc' => 'application/x-nschat',
        'oda' => 'application/oda',
        'odb' => 'application/vnd.oasis.opendocument.database',
        'odc' => 'application/vnd.oasis.opendocument.chart',
        'odf' => 'application/vnd.oasis.opendocument.formula',
        'odg' => 'application/vnd.oasis.opendocument.graphics',
        'odi' => 'application/vnd.oasis.opendocument.image',
        'odm' => 'application/vnd.oasis.opendocument.text-master',
        'odp' => 'application/vnd.oasis.opendocument.presentation',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
        'odt' => 'application/vnd.oasis.opendocument.text',
        'ofml' => 'application/fml',
        'oga' => 'audio/ogg',
        'ogg' => 'audio/x-oggvorbis',
        'ogv' => 'video/ogg',
        'ogx' => 'application/ogg',
        'otg' => 'application/vnd.oasis.opendocument.graphics-template',
        'oth' => 'application/vnd.oasis.opendocument.text-web',
        'otp' => 'application/vnd.oasis.opendocument.presentation-template',
        'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
        'ott' => 'application/vnd.oasis.opendocument.text-template',
        'oxt' => 'application/vnd.openofficeorg.extension',
        'p12' => 'application/x-pkcs12',
        'p7b' => 'application/x-pkcs7-certificates',
        'p7c' => 'application/x-pkcs7-mime',
        'p7m' => 'application/x-pkcs7-mime',
        'p7r' => 'application/x-pkcs7-certreqresp',
        'p7s' => 'application/x-pkcs7-signature',
        'pac' => 'application/x-ns-proxy-autoconfig',
        'page' => 'application/x-coda',
        'pbd' => 'application/vnd.powerbuilder6',
        'pbm' => 'image/x-portable-bitmap',
        'pdb' => 'chemical/x-pdb',
        'pdf' => 'application/pdf',
        'pfa' => 'application/x-font-type1',
        'pfb' => 'application/x-font-type1',
        'pfm' => 'application/x-font-type1',
        'pfr' => 'application/font-tdpfr',
        'pfx' => 'application/x-pkcs12',
        'pgm' => 'image/x-portable-graymap',
        'pgn' => 'application/x-chess-pgn',
        'pgp' => 'application/x-pgp-plugin',
        'pgr' => 'text/parsnegar-document',
        'php' => 'application/x-httpd-php',
        'php3' => 'application/x-httpd-php3',
        'php4' => 'application/x-httpd-php',
        'pls' => 'application/pls',
        'png' => 'image/png',
        'pnm' => 'image/x-portable-anymap',
        'pot' => 'application/mspowerpoint',
        'ppm' => 'image/x-portable-pixmap',
        'pps' => 'application/mspowerpoint',
        'ppt' => 'application/mspowerpoint',
        'ppz' => 'application/mspowerpoint',
        'pqf' => 'application/x-cprplayer',
        'pqi' => 'application/cprplayer',
        'prz' => 'application/vnd.lotus-freelance',
        'ps' => 'application/postscript',
        'psr' => 'application/datawindow',
        'ptlk' => 'application/listenup',
        'push' => 'multipart/x-mixed-replace',
        'qd3' => 'x-world/x-3dmf',
        'qd3d' => 'x-world/x-3dmf',
        'qrt' => 'application/quest',
        'qt' => 'video/quicktime',
        'qti' => 'image/x-quicktime',
        'qtl' => 'application/x-quicktimeplayer',
        'ra' => 'audio/x-pn-realaudio',
        'ram' => 'audio/x-pn-realaudio',
        'rar' => 'application/x-rar-compressed',
        'ras' => 'image/x-cmu-raster',
        'rdf' => 'application/rdf+xml',
        'rgb' => 'image/x-rgb',
        'rip' => 'image/rip',
        'rm' => 'audio/x-pn-realaudio',
        'rmf' => 'audio/rmf',
        'rmm' => 'audio/x-pn-realaudio',
        'rms' => 'audio/x-realaudio-secure',
        'rng' => 'application/ringing-tones',
        'roff' => 'application/x-troff',
        'rpm' => 'audio/x-pn-realaudio-plugin',
        'rrf' => 'application/x-InstallFromTheWeb',
        'rss' => 'application/rss+xml',
        'rtc' => 'application/rtc',
        'rtf' => 'application/rtf',
        'rtx' => 'text/richtext',
        'sca' => 'application/x-supercard',
        'scert' => 'application/x-x509-server-cert',
        'scr' => 'application/octet-stream',
        'seq' => 'application/octet-stream-bin',
        'ser' => 'application/x-java-serialized-object',
        'sgm' => 'text/sgml',
        'sgml' => 'text/sgml',
        'sh' => 'application/x-sh',
        'shar' => 'application/x-shar',
        'shtml' => 'text/x-server-parsed-html',
        'shw' => 'application/presentations',
        'sig' => 'application/pgp-signature',
        'silo' => 'model/mesh',
        'sit' => 'application/x-stuffit',
        'sjf' => 'application/octet-stream',
        'skd' => 'application/x-koan',
        'skm' => 'application/x-koan',
        'skp' => 'application/x-koan',
        'skt' => 'application/x-koan',
        'smi' => 'application/smil',
        'smil' => 'application/smil',
        'sml' => 'application/smil',
        'smp' => 'application/studiom',
        'snd' => 'audio/basic',
        'so' => 'application/octet-stream',
        'spc' => 'text/x-speech',
        'spl' => 'application/futuresplash',
        'spr' => 'application/x-sprite',
        'sprite' => 'application/x-sprite',
        'sql' => 'text/plain',
        'sqlfrag' => 'text/plain',
        'src' => 'application/x-wais-source',
        'ssi' => 'text/x-server-parsed-html',
        'stk' => 'application/hstu',
        'stream' => 'audio/x-qt-stream',
        'sv4cpio' => 'application/x-sv4cpio',
        'sv4crc' => 'application/x-sv4crc',
        'svf' => 'image/vnd',
        'svg' => 'image/svg+xml',
        'svh' => 'image/svh',
        'svr' => 'x-world/x-svr',
        'swa' => 'application/x-director',
        'swf' => 'application/x-shockwave-flash',
        't' => 'application/x-troff',
        'talk' => 'application/talker',
        'tar' => 'application/x-tar',
        'tbk' => 'application/toolbook',
        'tcl' => 'application/x-tcl',
        'tex' => 'application/x-tex',
        'texi' => 'application/x-texinfo',
        'texinfo' => 'application/x-texinfo',
        'text' => 'text/plain',
        'tgz' => 'application/gzip',
        'tif' => 'image/tiff',
        'tiff' => 'image/tiff',
        'tld' => 'text/xml',
        'tlk' => 'application/x-tlk',
        'tmv' => 'application/x-Parable-Thing',
        'torrent' => 'application/x-bittorrent',
        'tr' => 'application/x-troff',
        'troff' => 'application/x-troff',
        'tsi' => 'audio/tsplayer',
        'tsp' => 'application/dsptype',
        'tsq' => 'application/timestamp-query',
        'tsr' => 'application/timestamp-reply',
        'tsv' => 'text/tab-separated-values',
        'ttf' => 'font/ttf',
        'txt' => 'text/plain',
        'u98' => 'urdu/urdu98',
        'ucert' => 'application/x-x509-user-cert',
        'uin' => 'application/x-icq',
        'usercert' => 'application/x-x509-user-cert',
        'usr' => 'application/x-x509-user-cert',
        'ustar' => 'application/x-ustar',
        'vbd' => 'application/activexdocument',
        'vcd' => 'application/x-cdlink',
        'vcf' => 'text/x-vcard',
        'vew' => 'application/groupwise',
        'vgm' => 'video/x-videogram',
        'vgp' => 'video/x-videogram-plugin',
        'vgx' => 'video/x-videogram',
        'viv' => 'video/vnd.vivo',
        'vivo' => 'video/vnd.vivo',
        'vmd' => 'application/vocaltec-media-desc',
        'vmf' => 'application/vocaltec-media-file',
        'vmi' => 'application/x-dreamcast-vms-info',
        'vms' => 'application/x-dreamcast-vms',
        'vox' => 'audio/voxware',
        'vpa' => 'application/vpa',
        'vqe' => 'audio/x-twinvq-plugin',
        'vqf' => 'audio/x-twinvq',
        'vql' => 'audio/x-twinvq',
        'vrml' => 'model/vrml',
        'vrt' => 'x-world/x-vrt',
        'vts' => 'workbook/formulaone',
        'vtts' => 'workbook/formulaone',
        'vxml' => 'application/voicexml+xml',
        'w60' => 'application/wordperfect6.0',
        'w61' => 'application/wordperfect6.1',
        'waf' => 'plugin/wanimate',
        'wan' => 'plugin/wanimate',
        'war' => 'application/java-archive',
        'wav' => 'audio/wav',
        'wax' => 'audio/x-ms-wax',
        'wbmp' => 'image/vnd.wap.wbmp',
        'wbxml' => 'application/vnd.wap.wbxml',
        'wi' => 'image/wavelet',
        'wid' => 'application/x-DemoShield',
        'wis' => 'application/x-InstallShield',
        'wlt' => 'application/x-mswallet',
        'wm' => 'video/x-ms-wm',
        'wma' => 'audio/x-ms-wma',
        'wmd' => 'application/x-ms-wmd',
        'wmf' => 'image/x-wmf',
        'wml' => 'x-world/x-vrml',
        'wmlc' => 'application/vnd.wap.wmlc',
        'wmls' => 'text/vnd.wap.wmlscript',
        'wmlsc' => 'application/vnd.wap.wmlscriptc',
        'wmv' => 'video/x-ms-wmv',
        'wmx' => 'video/x-ms-wmx',
        'wmz' => 'application/x-ms-wmz',
        'wp' => 'application/wordperfect',
        'wp5' => 'application/wordperfect',
        'wpd' => 'application/wordperfect5.1',
        'wri' => 'application/write',
        'wrl' => 'x-world/x-vrml',
        'wrz' => 'x-world/x-vrml',
        'wtx' => 'audio/wtx',
        'wvx' => 'video/x-ms-wvx',
        'xaml' => 'application/xaml+xml',
        'xbm' => 'image/x-xbitmap',
        'xdr' => 'video/x-videogram',
        'xht' => 'application/xhtml+xml',
        'xhtml' => 'application/xhtml+xml',
        'xif' => 'image/vnd.xiff',
        'xla' => 'application/vnd.ms-excel',
        'xlc' => 'application/vnd.ms-excel',
        'xlm' => 'application/vnd.ms-excel',
        'xls' => 'application/vnd.ms-excel',
        'xlt' => 'application/vnd.ms-excel',
        'xlw' => 'application/vnd.ms-excel',
        'xml' => 'application/xml',
        'xmlfrag' => 'text/plain',
        'xpi' => 'application/x-xpinstall',
        'xpm' => 'image/x-xpixmap',
        'xsl' => 'application/xml',
        'xslt' => 'application/xslt+xml',
        'xul' => 'application/vnd.mozilla.xul+xml',
        'xwd' => 'image/x-xwindowdump',
        'xyz' => 'chemical/x-xyz',
        'z' => 'application/x-compress',
        'zip' => 'application/zip',
        'zpa' => 'application/pcphoto'
    );
    
    /**
     * Get file mime type
     *
     * @param string $file path of file name
     *
     * @return string
     */
    public static function getFromFile($file)
    {
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        
        return array_key_exists($ext, self::$mimeexts) ? self::$mimeexts[$ext] : 'application/octet-stream';
    }
}
